package com.service.abolishing.controller;

import com.service.abolishing.entity.UserInfo;
import com.service.abolishing.service.UserInfoService;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 渔港用户表(UserInfo)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:41
 */
@RestController
@RequestMapping("userInfo")
@Api(value = "渔民信息接口",tags = "渔民信息接口")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 获取渔民信息
     * @param
     * @return
     */
    @GetMapping(value = "queryAllFisherMan",produces="application/json")
    @ApiOperation(value = "获取渔民信息")
    @ResponseBody
    public Result loginOut(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) { // 缓存是否存在
                UserInfo userInfo = new UserInfo();
                userInfo.setManageInfoId(userId);
                List<UserInfo> userInfoList = userInfoService.queryAll(userInfo);
                return Result.ok().setData(userInfoList);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }else {
            return Result.failure(USER_NOT_LOGIN,"用户未登录");
        }
    }
}
